`
https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
var lowestCommonAncestor = function (root, p, q) {
  const find = (root, num1, num2) => {
    if (root === null) return null

    // 前序位置
    if (root.val === num1 || root.val === num2) {
      // 如果遇到目标值，直接返回
      return root
    }

    const left = find(root.left, num1, num2)
    const right = find(root.right, num1, num2)

    // 后序位置，已经知道左右子树是否存在目标值
    if (left && right) {
      return root
    }

    return left ? left : right
  }

  return find(root, p.val, q.val)
};